package com.dap.system.office.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.dap.common.core.exception.CustomException;
import com.dap.common.core.utils.DateUtils;
import com.dap.common.core.utils.SecurityUtils;
import com.dap.common.core.utils.StringUtils;
import com.dap.system.office.domain.Grade;
import com.dap.system.office.domain.Inpot;
import com.dap.system.office.domain.TClass;
import com.dap.system.office.domain.vo.TClassSelectorVo;
import com.dap.system.office.domain.vo.TSemesterSelectorVo;
import com.dap.system.office.domain.vo.TStudentSelectorVo;
import com.dap.system.office.mapper.TClassMapper;
import com.dap.system.office.mapper.TSemesterMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dap.system.office.mapper.TStudentMapper;
import com.dap.system.office.domain.TStudent;
import com.dap.system.office.service.ITStudentService;

import javax.annotation.Resource;

/**
 * 学生Service业务层处理
 *
 * @author dap
 * @date 2021-11-11
 */
@Service
public class TStudentServiceImpl implements ITStudentService
{
    @Resource
    private TStudentMapper tStudentMapper;
    @Resource
    private TClassMapper tClassMapper;
    @Resource
    private TSemesterMapper tSemesterMapper;

    /**
     * 查询学生
     *
     * @param id 学生ID
     * @return 学生
     */
    @Override
    public TStudent selectTStudentById(Long id)
    {
        return tStudentMapper.selectTStudentById(id);
    }

    /**
     * 查询学生列表
     *
     * @param tStudent 学生
     * @return 学生
     */
    @Override
    public List<TStudent> selectTStudentList(TStudent tStudent)
    {
        return tStudentMapper.selectTStudentList(tStudent);
    }

    /**
     * 分页查询学生列表
     *
     * @param tStudent 学生
     * @return 学生
     */
    @Override
    public List<TStudent> selectTStudentListPage(TStudent tStudent)
    {
        PageHelper.startPage(tStudent.getPageNum(),tStudent.getPageSize());
        List<TStudent> list = tStudentMapper.selectTStudentList(tStudent);
        PageInfo<TStudent> tPageInfo = new PageInfo<>(list);
        List<TStudent> dataList = tPageInfo.getList();
        return dataList;
    }
    /**
     * 新增学生
     *
     * @param tStudent 学生
     * @return 结果
     */
    @Override
    public int insertTStudent(TStudent tStudent) {
        TStudent student=new TStudent();
        student.setStuId(tStudent.getStuId());
        student.setStuIdNumber(tStudent.getStuIdNumber());
        student.setId(tStudent.getId());
        int rows = tStudentMapper.selectTstudentByNumberOrId(student);
        if (rows>0) {
            throw new CustomException("请输入正确的学号或身份证号");
        }
        tStudent.setCreateTime(DateUtils.getNowDate());
        tStudent.setDelFlag("0");
        tStudent.setCreateBy(SecurityUtils.getUsername());
        return tStudentMapper.insertTStudent(tStudent);
    }
    /**
     * 修改学生
     *
     * @param tStudent 学生
     * @return 结果
     */
    @Override
    public int updateTStudent(TStudent tStudent)
    {
        TStudent student=new TStudent();
        student.setStuId(tStudent.getStuId());
        student.setStuIdNumber(tStudent.getStuIdNumber());
        student.setId(tStudent.getId());
        int row = tStudentMapper.selectTstudentByNumberOrId(student);
        if (row >0 ){
            throw new CustomException("该学生已存在，修改失败");
        }
            tStudent.setUpdateBy(SecurityUtils.getUsername());
            tStudent.setUpdateTime(DateUtils.getNowDate());

        TClass tClass = new TClass();
        tClass.setGraId(tStudent.getGraId());
        tClass.setSemId(tStudent.getSemId());
        tClass.setClassName(tStudent.getClassName());
        List<TClass> tClasses = tClassMapper.selectTClassList(tClass);
        if(tClasses == null || tClasses.size() == 0) {
            throw new CustomException("所选择的班级不存在");
        }
        tStudent.setClassId(Integer.parseInt(tClasses.get(0).getId().toString()));
        return tStudentMapper.updateTStudent(tStudent);
        }

    /**
     * 批量删除学生
     *
     * @param ids 需要删除的学生ID
     * @return 结果
     */
    @Override
    public int deleteTStudentByIds(Long[] ids)
    {
        return tStudentMapper.deleteTStudentByIds(ids);
    }

    /**
     * 删除学生信息
     *
     * @param id 学生ID
     * @return 结果
     */
    @Override
    public int deleteTStudentById(Long id)
    {
        return tStudentMapper.deleteTStudentById(id);
    }

    @Override
    public List<TStudentSelectorVo> selectTStudentNameList(TStudent tStudent) {
        return tStudentMapper.selectAllTStudentNameList(tStudent);
    }

    @Override
    public String importStudent(List<TStudent> studentList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(studentList) || studentList.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        } else {
            for (TStudent students : studentList) {
                TStudent student = tStudentMapper.selectTStudentByCode(students.getStuId());
                if (student != null) {
                    Inpot byInfo = tSemesterMapper.findByInfo(students.getClassName(), students.getGradeName(), students.getSemName());
                    TStudent newStudent = new TStudent();
                    newStudent.setId(student.getId());
                    newStudent.setStuId(students.getStuId());
                    newStudent.setStuName(students.getStuName());
                    newStudent.setStuBirthday(students.getStuBirthday());
                    if(students.getStuSex().equals("男")){
                        newStudent.setStuSex("1");
                    }else{
                        newStudent.setStuSex("0");
                    }
                    newStudent.setStuIdNumber(students.getStuIdNumber());
                    newStudent.setNation(students.getNation());
                    newStudent.setAge(students.getAge());
                    newStudent.setPhoto(students.getPhoto());
                    newStudent.setClassId(byInfo.getClassId());
                    newStudent.setGraId(byInfo.getGradeId());
                    newStudent.setSemId(byInfo.getSemesterId());
                    newStudent.setRemark(students.getRemark());
                    newStudent.setUpdateBy(SecurityUtils.getUsername());
                    newStudent.setUpdateTime(DateUtils.getNowDate());
                    newStudent.setDelFlag("0");
                    tStudentMapper.updateTStudent(newStudent);
                } else {
                    Inpot byInfo = tSemesterMapper.findByInfo(students.getClassName(), students.getGradeName(), students.getSemName());
                    TStudent newStudent = new TStudent();
                    newStudent.setStuId(students.getStuId());
                    newStudent.setStuName(students.getStuName());
                    newStudent.setStuBirthday(students.getStuBirthday());
                    if(students.getStuSex().equals("男")){
                        newStudent.setStuSex("1");
                    }else{
                        newStudent.setStuSex("0");
                    }
                    newStudent.setStuIdNumber(students.getStuIdNumber());
                    newStudent.setNation(students.getNation());
                    newStudent.setAge(students.getAge());
                    newStudent.setPhoto(students.getPhoto());
                    newStudent.setClassId(byInfo.getClassId());
                    newStudent.setGraId(byInfo.getGradeId());
                    newStudent.setSemId(byInfo.getSemesterId());
                    newStudent.setRemark(students.getRemark());
                    newStudent.setCreateTime(DateUtils.getNowDate());
                    newStudent.setCreateBy(SecurityUtils.getUsername());
                    newStudent.setDelFlag("0");
                    tStudentMapper.insertTStudent(newStudent);
                }
            }
            return "导入成功";
        }
    }
}
